package com.wnxy.sddfp.system.listener;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wnxy.sddfp.common.model.dto.bank.BankCardRequestDto;
import com.wnxy.sddfp.system.entity.UserCard;
import com.wnxy.sddfp.system.entity.UserDetail;
import com.wnxy.sddfp.system.service.IUserCardService;
import com.wnxy.sddfp.system.service.IUserDetailService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

/**
 * @author yellow Doctor
 * @date 2025-03-28
 * @desc 蜗牛创想：www.woniuxy.com
 */
@Component
public class UpdateBindCardListener {

    @Autowired
    private IUserCardService userCardService;

    @Autowired
    private IUserDetailService userDetailService;

    @RabbitListener(queues = "sddfp-queue-bind")
    public void updateBindCard(@Payload BankCardRequestDto bankCardRequestDto){

        //查询用户详情
        String userCode = bankCardRequestDto.getUserCode();
        LambdaQueryWrapper<UserDetail> userDetailLambdaQueryWrapper = Wrappers.lambdaQuery(UserDetail.class);
        userDetailLambdaQueryWrapper.eq(UserDetail::getUserCode,userCode);
        UserDetail userDetail = userDetailService.getOne(userDetailLambdaQueryWrapper);

        //更新 user_card 表 (cardNO,bankTitle 判断条件是 userId)
        LambdaUpdateWrapper<UserCard> userCardLambdaUpdateWrapper = Wrappers.lambdaUpdate(UserCard.class)
                .set(UserCard::getBankNo, bankCardRequestDto.getBankNo())
                .set(UserCard::getBankTitle, bankCardRequestDto.getBankTitle())
                .eq(UserCard::getUserId, userDetail.getId());
        userCardService.update(userCardLambdaUpdateWrapper);

        //更新  user_detail 表 CardBindStatus
        userDetailService.update(
                Wrappers.lambdaUpdate(UserDetail.class)
                        .set(UserDetail::isCardBindStatus, true)  //绑卡字段
                        .eq(UserDetail::getUserCode, userCode)
        );
    }
}